package com.hxkcloud.nurse.popup;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.beans.AddressBean;
import com.hxkcloud.nurse.dborm.AddressDao;
import com.hxkcloud.nurse.utils.BrightnessUtils;
import com.hxkcloud.nurse.wheelview.OnWheelScrollListener;
import com.hxkcloud.nurse.wheelview.WheelView;
import com.hxkcloud.nurse.wheelview.adapter.ArrayWheelAdapter;

import java.util.List;

/**
 * Created by william_sun on 2016-11-03.
 * Description:省市区连选
 */
public class AreaSelectPop extends PopupWindow implements View.OnClickListener, OnWheelScrollListener {
    private Activity context;
    private WheelView wheel_province;
    private WheelView wheel_city;
    private WheelView wheel_area;

    private int curProvince;
    private int curCity;
    private int curArea;

    private Button bt_cancel;
    private Button bt_sure;

    private AddressDao addressDao;

    private List<AddressBean> psList;
    private String[] ps;
    private ArrayWheelAdapter adapterPs;
    private List<AddressBean> csList;
    private String[] cs;
    private ArrayWheelAdapter adapterCs;
    private List<AddressBean> asList;
    private String[] as;
    private ArrayWheelAdapter adapterAs;

    public AreaSelectPop(Activity context, onAreaSelectListener listener) {
        this.context = context;
        this.addressDao = new AddressDao(context);
        this.areaSelectListener = listener;

        View rootView = LayoutInflater.from(context).inflate(R.layout.popup_selectymd, null);
        setContentView(rootView);

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable(0x00000000));

        setAnimationStyle(R.style.popupwindow_anim_default);
        initViews(rootView);
        initDatas();
        initListener();
    }

    private void initViews(View rootView) {
        wheel_province = (WheelView) rootView.findViewById(R.id.wheel_selectymd_year);
        wheel_city = (WheelView) rootView.findViewById(R.id.wheel_selectymd_month);
        wheel_area = (WheelView) rootView.findViewById(R.id.wheel_selectymd_day);
        bt_cancel = (Button) rootView.findViewById(R.id.bt_selectymd_cancel);
        bt_sure = (Button) rootView.findViewById(R.id.bt_selectymd_sure);
    }

    private void initDatas() {
        wheel_province.setCyclic(false);//是否可循环滑动
        wheel_city.setCyclic(false);
        wheel_area.setCyclic(false);

        //设置显示行数
        wheel_province.setVisibleItems(7);
        wheel_city.setVisibleItems(7);
        wheel_area.setVisibleItems(7);

        psList = addressDao.getAddress("0");
        ps = new String[psList.size()];
        for (int i = 0; i < psList.size(); i++) {
            ps[i] = psList.get(i).getName();
        }
        adapterPs = new ArrayWheelAdapter(context, ps);
        wheel_province.setViewAdapter(adapterPs);
        wheel_province.setCurrentItem(0);

        csList = addressDao.getAddress(psList.get(0).getItemId());
        cs = new String[csList.size()];
        for (int i = 0; i < csList.size(); i++) {
            cs[i] = csList.get(i).getName();
        }
        adapterCs = new ArrayWheelAdapter(context, cs);
        wheel_city.setViewAdapter(adapterCs);
        wheel_city.setCurrentItem(0);

        asList = addressDao.getAddress(csList.get(0).getItemId());
        as = new String[asList.size()];
        for (int i = 0; i < asList.size(); i++) {
            as[i] = asList.get(i).getName();
        }
        adapterAs = new ArrayWheelAdapter(context, as);
        wheel_area.setViewAdapter(adapterAs);
        wheel_area.setCurrentItem(0);
    }

    private void initListener() {
        bt_sure.setOnClickListener(this);
        bt_cancel.setOnClickListener(this);
        wheel_province.addScrollingListener(this);
        wheel_city.addScrollingListener(this);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                BrightnessUtils.lightOn(context);
            }
        });
    }

    public void show(View parent) {
        wheel_province.setCurrentItem(0);
        wheel_city.setCurrentItem(0);
        wheel_area.setCurrentItem(0);
        onScrollingFinished(wheel_province);
        showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        BrightnessUtils.lightOff(context);
    }

    @Override
    public void onClick(View v) {
        dismiss();
        switch (v.getId()) {
            case R.id.bt_selectymd_sure:
                if (areaSelectListener != null) {
                    areaSelectListener.onSelect(ps[wheel_province.getCurrentItem()], cs[wheel_city.getCurrentItem()], as[wheel_area.getCurrentItem()], asList.get(wheel_area.getCurrentItem()).getItemId());
                }
                break;
            case R.id.bt_selectymd_cancel:

                break;
        }
    }

    private onAreaSelectListener areaSelectListener;

    public interface onAreaSelectListener {
        void onSelect(String p, String c, String a, String id);
    }

    @Override
    public void onScrollingStarted(WheelView wheel) {

    }

    @Override
    public void onScrollingFinished(WheelView wheel) {
        if (wheel.getId() == R.id.wheel_selectymd_year) {
            String pId = psList.get(wheel_province.getCurrentItem()).getItemId();
            csList = addressDao.getAddress(pId);
            cs = new String[csList.size()];
            for (int i = 0; i < csList.size(); i++) {
                cs[i] = csList.get(i).getName();
            }
            adapterCs = new ArrayWheelAdapter(context, cs);
            wheel_city.setViewAdapter(adapterCs);
            wheel_city.setCurrentItem(0);

            asList = addressDao.getAddress(csList.get(0).getItemId());
            as = new String[asList.size()];
            for (int i = 0; i < asList.size(); i++) {
                as[i] = asList.get(i).getName();
            }
            adapterAs = new ArrayWheelAdapter(context, as);
            wheel_area.setViewAdapter(adapterAs);
            wheel_area.setCurrentItem(0);
        } else if (wheel.getId() == R.id.wheel_selectymd_month) {
            String cId = csList.get(wheel_city.getCurrentItem()).getItemId();
            asList = addressDao.getAddress(cId);
            as = new String[asList.size()];
            for (int i = 0; i < asList.size(); i++) {
                as[i] = asList.get(i).getName();
            }
            adapterAs = new ArrayWheelAdapter(context, as);
            wheel_area.setViewAdapter(adapterAs);
            wheel_area.setCurrentItem(0);
        }
    }
}
